<?php

namespace app\admin\controller\shop;

use app\admin\model\City;
use app\admin\model\User;
use app\common\controller\Backend;
use fast\Random;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 商城供应商
 *
 * @icon fa fa-circle-o
 */
class Mallsupplier extends Backend
{

    /**
     * Mallsupplier模型对象
     * @var \app\admin\model\Mallsupplier
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Mallsupplier;

    }

   public function selectpage()
   {
       return parent::selectpage(); // TODO: Change the autogenerated stub
   }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */





    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);


        $name = $params['name'];
        $mobile = $params['mobile'];
        //判断手机号
        $pattern = '/^1[3-9]\d{9}$/';
        if (!preg_match($pattern, $mobile)) {
            $this->error('手机号格式不正确，请检查');
        }
        $username = $params['username'];
        $password = $params['password'];
        $mark = $params['mark'];

        $userModel = new User();
        $u = $userModel->where('mobile',$mobile)->find();

        if(!$u){
            $db = array();
            $db['group_id'] = 1;
            $db['username'] = $username;
            $db['createtime'] = time();
            $db['status'] = 'normal';
            $salt = Random::alnum();
            $db['mobile'] = $mobile;
            $newpassword =md5(md5($password) . $salt);
            $db['salt'] = $salt;
            $db['password'] = $newpassword;


            $userModel->insertGetId($db);
        }
        $data = $this->model->where('mobile',$mobile)->find();
        if($data)$this->error('此手机号供应商已存在');
        $db = array();
        $db['username'] = $username;
        $db['name'] = $name;
        $db['password'] = $password;
        $db['mark'] = $mark;
        $db['mobile'] = $mobile;

        $this->model->insert($db);

        $this->success('添加成功');
    }



    /**
     * 编辑
     */
    public function edit($ids = null)
    {

        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                $name = $params['name'];
                $mobile = $params['mobile'];
                $username = $params['username'];
                $password = $params['password'];
                $mark = $params['mark'];

                $userModel = new User();
                $user = $userModel->where('mobile',$mobile)->find();
                if(empty($user)){
                    $this->error('该手机号不存在，请先添加');
                }

                $newpassword =md5(md5($password) . $user['salt']);
                $userModel->where('id',$user['id'])->update(['password'=>$newpassword]);

                $db = array();
                $db['username'] = $username;
                $db['name'] = $name;
                $db['password'] = $password;
                $db['mark'] = $mark;
                $db['mobile'] = $mobile;
                $this->model->where('id',$ids)->update($db);
                $this->success();

            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        //$this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }



}
